/*
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "./waypoint.h"
#include <time.h>

Waypoint::Waypoint()
{
  position   = Position();
  time       = QTime();
  date       = QDate();
  alt        = 0;
  distance   = 0;
  heart_rate = 0;
  cadence    = 0;
  sensor     = false;
}

Waypoint::Waypoint(D304* w)
{
  time_t ti    = TIME_OFFSET + w->time;
  tm *t      = localtime(&ti);
  // tm_year: years since 1900
  date = QDate(t->tm_year + 1900, t->tm_mon + 1, t->tm_mday);
  time = QTime(t->tm_hour, t->tm_min, t->tm_sec);


  position   = Position(&w->posn);
  //time       = QTime().addSecs(w->time);
  alt        = w->alt;
  distance   = w->distance;
  heart_rate = w->heart_rate;
  cadence    = w->cadence;
  sensor     = w->sensor;
}

QString Waypoint::toString()
{
  QString string("");
  string += "[ WAYPOINT";
  string += " position:";
  string += position.toString();
  string += " time:";
  string += time.toString();
  string += " date:";
  string += date.toString();
  string += " total distance:";
  if (distance > 1.0e24)
    string += " invalid";
  else
    string += QVariant(distance).toString();
  if (heart_rate > 0) {
    string += " heart rate:";
    string += QVariant(heart_rate).toString();
  }
  if (cadence != 255) {
    string += " cadence:";
    string += QVariant(cadence).toString();
  }
  string += " sensor:";
  string += QVariant(sensor).toString();
  string += " ]";
  return string;
}

//
//
//  getters/setters
//
//

QDate Waypoint::getDate()
{
  return date;
}

Position Waypoint::getPosition()
{
        return position;
}
//TODO: copy
// TODO: also, by reference
void Waypoint::setPosition(Position val)
{
        position = val;
}
QTime Waypoint::getTime()
{
        return time;
}
void Waypoint::setTime(QTime val)
{
        time = val;
}
float32 Waypoint::getAlt()
{
        return alt;
}
void Waypoint::setAlt(float32 val)
{
        alt = val;
}
float32 Waypoint::getDistance()
{
        return distance;
}
void Waypoint::setDistance(float32 val)
{
        distance = val;
}
uint8 Waypoint::getHeart_rate()
{
        return heart_rate;
}
void Waypoint::setHeart_rate(uint8 val)
{
        heart_rate = val;
}
uint8 Waypoint::getCadence()
{
        return cadence;
}
void Waypoint::setCadence(uint8 val)
{
        cadence = val;
}
bool Waypoint::getSensor()
{
        return sensor;
}
void Waypoint::setSensor(bool val)
{
        sensor = val;
}

